<?php
namespace Home\Controller;

use Think\Controller;
use Home\Model\RecycleOrderModel;
use Home\Model\MemberModel;

/**
 * 师傅端controller
 */

class MasterController extends Controller{

	private $RecycleOrder = null;
	private $mid;
	function __construct(){
		parent::__construct();
//		$_SESSION['master_id'] = 129;
		if($_SESSION['master_id']){
			$this->mid = $_SESSION['master_id'];
		}else{
			returnMobileJson(0,'未登录');
		}
		$this->RecycleOrder = new RecycleOrderModel();
	}

	/**
	 * 获取师傅回收订单列表
	 */
	public function get_master_order(){
		
		$master_order = $this->RecycleOrder->get_master_order($this->mid);
		if(!$master_order){
			returnMobileJson(201,'没有数据');
		}else{
			returnMobileJson(200,'获取成功',$master_order);
		}
	}

	/**
	 * 获取回收订单
	 */
	public function get_order(){
		$rid=$_POST['rid'];
		if(empty($rid)){
			returnMobileJson(201,'参数错误');
		}
		$order = $this->RecycleOrder->get_order($rid);
		returnMobileJson(200,'获取成功',$order);
	}

	/**
	 * 师傅确定接单
	 */
	public function comfirm_order(){
		$rid=$_POST['rid'];
		if(empty($rid)){
			returnMobileJson(201,'参数错误');
		}
		// 更改订单状态
		$data['status']=2;
		if($this->RecycleOrder->set_order($data,$rid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
	 * 师傅修改价格
	 */
	public function edit_price(){
		$rid=$_POST['rid'];
		$data['price']=$_POST['price'];
		if(empty($rid)||empty($data['price'])){
			returnMobileJson(201,'参数错误');
		}
		$data['status']=3;
		if($this->RecycleOrder->set_order($data,$rid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	/**
	 * 师傅确认回收
	 */
	public function comfirm_recycle(){
		$rid=$_POST['rid'];
		if(empty($rid)){
			returnMobileJson(201,'参数错误');
		}
		$data['status']=3;
		if($this->RecycleOrder->set_order($data,$rid)){
			returnMobileJson(200,'修改成功');
		}else{
			returnMobileJson(202,'修改失败');
		}
	}

	public function get_master_dashboard(){
		$data['order_count'] = $this->RecycleOrder->master_order_count(1);
		$Member = new MemberModel();
		$member = $Member->get_member($this->mid);
		$data['money'] = $member['m_money'];
		returnMobileJson(200,'获取成功',$data);
	}


	/**
	 * 短信绑定微信公众号
	 */
	public function get_phone_code(){
		$phone = $_REQUEST['phone'];
		if(!preg_match('/^1[34578]{1}\d{9}$/',$phone)){
			returnMobileJson(201,'手机号码格式错误');
		}
		$Merber = new MemberModel();
		if($Merber->determine_whether_can_be_sent($phone)){
			$postData = array();
			$postData['uid'] = 2;
			$postData['user_key'] = 'efcd9440dde0644df6b2cb1bcf1fc549';
			$postData['param_json'] = json_encode(array(
					'sign_name' => 'GO机网',//模板签名
					'product' => 'GO机网',//短信参数
					'template_code' => 'SMS_23910016',//短信模板参数
					'phone' => $phone//发送的手机号码
			));

			echo httpPost('http://sms.txiaoyuan.cn/index.php/Home/Control/control?app=SMS&function=autoSendSMS',$postData);
		}
	}

	/**
	 * 绑定微信账号
	 */
	public function binding_wechat_id(){
		$checkCode = $_REQUEST['checkCode'];
		$phone = $_REQUEST['phone'];

		if(verification_code_is_correct($phone,$checkCode)){
			$Merber = new MemberModel();
			if($Merber->binding_wechat_id($phone)){
				returnMobileJson(200,'验证成功');
			}else{
				returnMobileJson(-2,'非法操作');
			}
		}else{
			returnMobileJson(-2,'验证失败');
		}


	}


	/**
	 * 获取师傅的个人信息
	 */
	public function access_to_master_information(){
		$memberdb = new MemberModel();
		$data = $memberdb->access_to_master_information($this->mid);
		if($data['member_id']){
			returnMobileJson(200,'获取成功',$data);
		}else{
			returnMobileJson(-2,'非法操作');
		}
	}
}